<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\AdminMenu;

class Base extends Controller
{
    protected $uid;
    protected $username;
    private $admin_menu;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        //判断是否登陆
        if (!session('uid')) {
            $this->error('您还没有登录, 请登录', 'admin/login/index');
        }

        $this->admin_menu = new AdminMenu;

        // 登录用户信息
        $this->uid = session('uid');
        $this->username = session('username');

        $rid = model('admin_user')->where('id', $this->uid)->value('role_id');
                       
        //菜单数据
        $data_menu = $this->makeMenu($rid);
       // print_json($data_menu);

        //当前活动菜单父id
        $controller = $request->controller();       
        $menu_url = strtolower('admin/'.preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $controller)).'/index';
   
        $active_pid =  $this->admin_menu->where('url', $menu_url)->value('pid');
        // var_dump($active_pid);
        // exit;
        //当前控制器及方法(转小写)
        $controller = strtolower($controller);
        $action = strtolower($request->action());

        //权限检查
        // if (!$this->checkAuthor($this->rid)) {
        //     $this->error('你无权限操作');
        // }
        
        //记录日志
        $this->addLog();

        // 模板输出
        $this->assign('data_menu', $data_menu);
        $this->assign('active_pid', $active_pid);
        $this->assign('controller', $controller);
        $this->assign('action', $action);
    }

    /**
     * 目录获取
     */
    private function makeMenu($rid)
    {
        if ($rid === 1) {
            $data = $this->admin_menu
                ->where('status=1')
                ->where('type!=2')
                ->field('id, pid, name, url, type, icon')
                ->select();
        } else {
            $permission_ids = model('admin_role')->where('id', $rid)->value('permission');
            $data = $this->admin_menu
                ->where('status=1')
                ->where('type!=2')
                ->where('id', 'IN', $permission_ids)
                ->field('id, pid, name, url, icon')
                ->select();
        }

        return obj_tree($data);
    }

    /**
     * 权限检查
     */
    // private function checkAuthor($rid)
    // {
    //     if (!$rid) {
    //         return false;
    //     }

    //     if ($rid==1) {
    //         return true;
    //     }

    //     $c = strtolower(request()->controller());
    //     $a = strtolower(request()->action());

    //     if (preg_match('/^public_/', $a)) {
    //         return true;
    //     }
    //     if ($c == 'index' && $a == 'index') {
    //         return true;
    //     }
    //     $menu = model('Menu')->getMyMenu($user_id);
    //     foreach ($menu as $k => $v) {
    //         if (strtolower($v['c']) == $c && strtolower($v['a']) == $a) {
    //             return true;
    //         }
    //     }
    //     return false;
    // }

    /**
     * 记录日志
     */
    private function addLog()
    {

        $data = array();
        $data['querystring'] = request()->query()?'?'.request()->query():'';
        $data['m'] = request()->module();
        $data['c'] = request()->controller();
        $data['a'] = request()->action();
        $data['userid'] = $this->uid;
        $data['username'] = $this->username;
        $data['ip'] = inet_aton(request()->ip());
        $arr = array('Index/index','Log/index','Menu/index');
        if (!in_array($data['c'].'/'.$data['a'], $arr)) {
            db('admin_log')->insert($data);
        }
    }
}
